package com.andyage.admin.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import com.andyage.admin.dao.AdUserMapper;
import com.andyage.admin.dao.AdteUserMapper;
import com.andyage.admin.service.IAdteUserService;
import com.andyage.util.GetUUID;
import com.andyage.util.JsonContent;
import com.andyage.util.TypeTransfor;
import com.andyage.admin.bean.AdteuserBean;
import com.andyage.admin.bean.AduserBean;

@Service
public class AdteUserServiceImpl implements IAdteUserService {
	public String CONTENT_TYPE = "text/html;charset=utf-8";
	@Resource
	private AdteUserMapper adteUserMapper;
	@Resource
	private AdUserMapper aduserMapper;
	@Override
	public void queryAllAdteuserByCondition(AdteuserBean userBean, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<AdteuserBean> list = adteUserMapper.queryAllAdteuserByCondition(userBean);
		int total = adteUserMapper.queryAllAdteuserCountByCondition(userBean);
		String jsonObject = JsonContent.getJsonArray(list, total);
		response.setContentType(CONTENT_TYPE);
		response.getWriter().println(jsonObject);

	}

	@Override
	public void updateAdteUserByUser(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String userName = request.getParameter("userName");
		String tuNickName = request.getParameter("userNickName");
		String userLogoUrl = request.getParameter("userLogoUrl");
		String tuAccount = request.getParameter("tuAccount");
		String userSex = request.getParameter("userSex");
		String tuUserId = request.getParameter("tuUserId");
		AduserBean aduser = new AduserBean();
		if(userName!=null&&!"".equals(userName)){
			aduser.setUserName(userName);
		}
		if(tuNickName!=null&&!"".equals(tuNickName)){
			aduser.setUserNickName(tuNickName);
			
		}
		if(userLogoUrl!=null&&!"".equals(userLogoUrl)){
			aduser.setUserLogoUrl(userLogoUrl);
		}
		if(tuAccount!=null&&!"".equals(tuAccount)){
			aduser.setUserTelephone(Long.valueOf(tuAccount));
			//aduser.setUserId(GetUUID.get());
		}
		if(userSex!=null && !"".equals(userSex)){
			if(TypeTransfor.isNumber(userSex)){
				aduser.setUserSex(Integer.valueOf(userSex));
			}else{
				aduser.setUserSex(Integer.valueOf(TypeTransfor.userSexTransfor(userSex)));
			}
		}
		if(tuUserId!=null&&!"".equals(tuUserId)){
			aduser.setUserId(tuUserId);
		}
		int flag = aduserMapper.updateAduserInfo(aduser);
		JSONObject json = new JSONObject();
		if (flag != 0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "修改成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "修改失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void insertAdteUser(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String userName = request.getParameter("userName");
		String tuNickName = request.getParameter("userNickName");
		String userLogoUrl = request.getParameter("userLogoUrl");
		String tuAccount = request.getParameter("tuAccount");
		String userSex = request.getParameter("userSex");
		AdteuserBean userBean = new AdteuserBean();
		AduserBean aduser = new AduserBean();
		if(userName!=null&&!"".equals(userName)){
			aduser.setUserName(userName);
		}
		if(tuNickName!=null&&!"".equals(tuNickName)){
			userBean.setTuNickName(tuNickName);
			aduser.setUserNickName(tuNickName);
		}
		if(userLogoUrl!=null&&!"".equals(userLogoUrl)){
			aduser.setUserLogoUrl(userLogoUrl);
		}
		if(tuAccount!=null&&!"".equals(tuAccount)){
			userBean.setTuUserId(GetUUID.get());
			userBean.setTuAccount(tuAccount);
			aduser.setUserRegisterTime(new Date());
			aduser.setUserTelephone(Long.valueOf(tuAccount));
			aduser.setUserId(userBean.getTuUserId());
		}
		if(userSex!=null && !"".equals(userSex)){
			aduser.setUserSex(Integer.valueOf(userSex));
		}
		aduser.setUserIsSponsor(true);
		aduserMapper.insertAduserInfo(aduser);//写入aduser表
		int flag = adteUserMapper.insertAdteUser(userBean);//写入adteuser表
		JSONObject json = new JSONObject();
		if (flag != 0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "添加成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "添加失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void querytuAccountByTel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String tuAccount = request.getParameter("tuAccount");
		List<AdteuserBean> list = adteUserMapper.querytuAccountByTel(tuAccount);
		JSONObject json = new JSONObject();
		if(!list.isEmpty()){
			json.accumulate("result", 1);
			json.accumulate("tips", "用户已存在");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

}
